<template>
  <div>
    <div class="search">
      <el-button type="primary" @click="target('add')">新增</el-button>
    </div>
    <div class="content">
      <el-table :data="data" style="width: 100%">
        <el-table-column prop="title" label="名称"> </el-table-column>
        <el-table-column prop="type" label="类型">
          <template v-slot="{ row }">
            {{ row.type == 1 ? "原创" : "转载" }}
          </template>
        </el-table-column>
        <el-table-column prop="reprintName" label="转载地点"> </el-table-column>
        <el-table-column prop="reprintUrl" label="转载地址"> </el-table-column>
        <el-table-column prop="tips" label="标签"> </el-table-column>
        <el-table-column prop="readNumber" label="阅读数"> </el-table-column>
        <el-table-column prop="createTime" label="创建时间">
          <template v-slot="{ row }">
            <div>{{ row.createTime | dateFormat }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="updateTime" label="更新时间">
          <template v-slot="{ row }">
            <div>{{ row.updateTime | dateFormat }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="bState" label="是否激活">
          <template v-slot="{ row }">
            {{ row.bState ? "是" : "否" }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <div>
              <el-button type="primary" @click="target('edit', row)"
                >修改</el-button
              >
              <el-button type="danger" @click="target('delect', row)"
                >删除</el-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          @current-change="current"
          background
          layout="prev, pager, next"
          :current-page="currentPage"
          :total="page.count"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  layout: "admin",
  computed: {
    currentPage() {
      return parseInt(parseInt(this.page.pageNo) + 1);
    },
  },
  data() {
    return {
      data: [],
      page: {
        pageSize: 10,
        pageNo: 0,
      },
    };
  },
  mounted() {
    this.target("init");
  },
  methods: {
    target(key, data) {
      switch (key) {
        case "init":
          this.getData();
          break;
        case "add":
          this.$router.push({
            path: "/admin/note/add/data",
          });
          break;
        case "edit":
          this.$router.push({
            path: `/admin/note/add/${data.noteId}`,
          });
          break;
        case "delect":
          this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
            .then((result) => {
              this.delect(data.noteId);
            })
            .catch((err) => {});
          break;
        default:
          break;
      }
    },
    async getData() {
      let data = await this.$axios.post("/admin/note/get/all", {
        page: this.page,
      });
      this.page = data.data.page;
      this.data = data.data.result;
    },
    async delect(bannerId) {
      let data = await this.$axios.delete(`/admin/note/delete/${bannerId}`, {
        page: this.page,
      });
      this.target("init");
      this.$message({
        type: "success",
        message: "删除成功!",
      });
    },
    current(index) {
      this.page.pageNo = index - 1;
      this.getData();
    },
  },
};
</script>

<style lang="scss" scoped>
</style>